import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getwidget/getwidget.dart';
import 'package:meal/constant/antd_icons.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/route/route.dart';
import 'package:meal/utils/colors.dart';
import 'package:meal/utils/styles.dart';
import 'package:meal/utils/tools.dart';

///
/// 编辑满赠活动
///
class FullEdit extends StatefulWidget {
  const FullEdit({Key? key}) : super(key: key);

  @override
  State<FullEdit> createState() => _FullEditState();
}

class _FullEditState extends State<FullEdit> {
  bool isNew = true;

  bool favorable = true;

  TextStyle get style {
    return StylesUtils.customTextStyle(color: ColorsUtils.color333, fontSize: Sizes.default13);
  }

  double get leftWidth {
    return Sizes.default90;
  }

  double get rowHeight {
    return Sizes.default60;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ToolsUtils.getDefaultBackgroundColor(),
      appBar: ToolsUtils.getDefaultBrnAppBar('编辑满赠活动',
          actions: ToolsUtils.actions('说明', pressed: () {
            Get.toNamed(RouteGet.marketFullExplain);
          })),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              margin: const EdgeInsets.only(top: Sizes.default20),
              padding:
                  const EdgeInsets.symmetric(horizontal: Sizes.default15, vertical: Sizes.default8),
              decoration: const BoxDecoration(color: Colors.white),
              child: ListBody(
                children: [
                  Container(
                    height: rowHeight,
                    alignment: Alignment.centerLeft,
                    child: Row(
                      children: [
                        Container(
                            width: leftWidth,
                            alignment: Alignment.centerLeft,
                            child: Text.rich(TextSpan(
                                text: '满赠规则',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color333)))),
                        Expanded(
                          child: InkWell(
                            child: Container(
                              height: Sizes.default45,
                              padding: const EdgeInsets.symmetric(horizontal: Sizes.default10),
                              alignment: Alignment.centerLeft,
                              decoration: BoxDecoration(
                                  color: ColorsUtils.rgba(247, 247, 247, 1),
                                  borderRadius:
                                      const BorderRadius.all(Radius.circular(Sizes.default10))),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text('添加规则2/5',
                                      style: StylesUtils.customTextStyle(
                                          fontSize: Sizes.default15, color: ColorsUtils.color797)),
                                  BrunoTools.getAssetSizeImage(BrnAsset.iconRightArrow, 16, 16)
                                ],
                              ),
                            ),
                            onTap: () {
                              Get.toNamed(RouteGet.marketFullRule);
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(Sizes.default5),
                        border: Border.all(color: ColorsUtils.rgba(228, 228, 228, 1))),
                    padding: const EdgeInsets.all(Sizes.default10),
                    child: Column(
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              '[NO.1]',
                              style: style,
                            ),
                            InkWell(
                                onTap: () {},
                                child: Icon(
                                  AntdIcons.delete,
                                  color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorFf6),
                                  size: Sizes.default15,
                                ))
                          ],
                        ),
                        Container(
                          margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                '赠送条件',
                                style: style,
                              ),
                              Text(
                                '满300',
                                style: style,
                              )
                            ],
                          ),
                        ),
                        Container(
                          margin: const EdgeInsets.only(bottom: Sizes.default10),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                '名称',
                                style: style,
                              ),
                              Text(
                                '苹果',
                                style: style,
                              )
                            ],
                          ),
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              '赠送数量',
                              style: style,
                            ),
                            Text(
                              '1',
                              style: style,
                            )
                          ],
                        ),
                      ],
                    ),
                  ),
                  Container(
                    margin: const EdgeInsets.only(top: Sizes.default15),
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(Sizes.default5),
                        border: Border.all(color: ColorsUtils.rgba(228, 228, 228, 1))),
                    padding: const EdgeInsets.all(Sizes.default10),
                    child: Column(
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              '[NO.2]',
                              style: style,
                            ),
                            InkWell(
                                onTap: () {},
                                child: Icon(
                                  AntdIcons.delete,
                                  color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorFf6),
                                  size: Sizes.default15,
                                ))
                          ],
                        ),
                        Container(
                          margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                '赠送条件',
                                style: style,
                              ),
                              Text(
                                '满500',
                                style: style,
                              )
                            ],
                          ),
                        ),
                        Container(
                          margin: const EdgeInsets.only(bottom: Sizes.default10),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                '名称',
                                style: style,
                              ),
                              Text(
                                '苹果',
                                style: style,
                              )
                            ],
                          ),
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              '赠送数量',
                              style: style,
                            ),
                            Text(
                              '2',
                              style: style,
                            )
                          ],
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: Sizes.default20),
              padding:
                  const EdgeInsets.symmetric(horizontal: Sizes.default15, vertical: Sizes.default8),
              decoration: const BoxDecoration(color: Colors.white),
              child: ListBody(
                children: [
                  Container(
                    height: rowHeight,
                    alignment: Alignment.centerLeft,
                    child: Row(
                      children: [
                        Container(
                            width: leftWidth,
                            alignment: Alignment.centerLeft,
                            child: Text.rich(TextSpan(children: [
                              TextSpan(
                                text: '赠总品数量',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                              ),
                            ]))),
                        Expanded(
                            child: Container(
                          height: Sizes.default45,
                          padding: const EdgeInsets.symmetric(horizontal: Sizes.default10),
                          margin: const EdgeInsets.only(right: Sizes.default15),
                          decoration: BoxDecoration(
                              color: ColorsUtils.rgba(247, 247, 247, 1),
                              borderRadius:
                                  const BorderRadius.all(Radius.circular(Sizes.default10))),
                          child: TextField(
                            controller: ToolsUtils.editingController('500'),
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color363, fontSize: Sizes.default15),
                            decoration: InputDecoration(
                                border: const OutlineInputBorder(
                                  borderSide: BorderSide.none,
                                ),
                                isCollapsed: true,
                                filled: true,
                                fillColor: Colors.transparent,
                                contentPadding: const EdgeInsets.symmetric(
                                    horizontal: Sizes.default10, vertical: Sizes.default10),
                                constraints: const BoxConstraints(minHeight: Sizes.default45),
                                hintText: '请输入总数量',
                                hintStyle: StylesUtils.customTextStyle(
                                    fontSize: Sizes.default14, color: ColorsUtils.color999)),
                            keyboardType: TextInputType.number,
                            onChanged: (String content) {},
                          ),
                        )),
                      ],
                    ),
                  ),
                  Container(
                    height: rowHeight,
                    alignment: Alignment.centerLeft,
                    child: Row(
                      children: [
                        Container(
                          width: leftWidth,
                          alignment: Alignment.centerLeft,
                          child: Text(
                            '商品类型',
                            style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                          ),
                        ),
                        Expanded(
                            child: Container(
                          alignment: Alignment.centerLeft,
                          child: Wrap(
                            spacing: Sizes.default10,
                            alignment: WrapAlignment.start,
                            crossAxisAlignment: WrapCrossAlignment.center,
                            children: [
                              InkWell(
                                onTap: () {
                                  setState(() {
                                    isNew = true;
                                  });
                                },
                                child: Container(
                                  alignment: Alignment.center,
                                  constraints: const BoxConstraints.expand(
                                      width: 110, height: Sizes.default30),
                                  decoration: changeBoxDecoration(isNew),
                                  child: Text(
                                    '店铺新用户',
                                    style: isNew
                                        ? StylesUtils.customTextStyle(color: ColorsUtils.colorFf6)
                                        : StylesUtils.customTextStyle(color: ColorsUtils.color333),
                                  ),
                                ),
                              ),
                              InkWell(
                                onTap: () {
                                  setState(() {
                                    isNew = false;
                                  });
                                },
                                child: Container(
                                  alignment: Alignment.center,
                                  constraints: const BoxConstraints.expand(
                                      width: 110, height: Sizes.default30),
                                  decoration: changeBoxDecoration(!isNew),
                                  child: Text('店铺所有用户',
                                      style: !isNew
                                          ? StylesUtils.customTextStyle(color: ColorsUtils.colorFf6)
                                          : StylesUtils.customTextStyle(
                                              color: ColorsUtils.color333)),
                                ),
                              ),
                            ],
                          ),
                        ))
                      ],
                    ),
                  ),
                  Container(
                    height: rowHeight,
                    alignment: Alignment.centerLeft,
                    child: Row(
                      children: [
                        Container(
                            width: leftWidth,
                            alignment: Alignment.centerLeft,
                            child: Text.rich(TextSpan(
                                text: '活动日期',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color333)))),
                        Expanded(
                          child: InkWell(
                            child: Container(
                              height: Sizes.default45,
                              padding: const EdgeInsets.symmetric(horizontal: Sizes.default10),
                              alignment: Alignment.centerLeft,
                              decoration: BoxDecoration(
                                  color: ColorsUtils.rgba(247, 247, 247, 1),
                                  borderRadius:
                                      const BorderRadius.all(Radius.circular(Sizes.default10))),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text('',
                                      style: StylesUtils.customTextStyle(
                                          fontSize: Sizes.default15, color: ColorsUtils.color797)),
                                  BrunoTools.getAssetSizeImage(BrnAsset.iconRightArrow, 16, 16)
                                ],
                              ),
                            ),
                            onTap: () {
                              ToolsUtils.openDateRangePicker(context, onConfirm:
                                  (DateTime startDateTime, DateTime endDateTime,
                                      List<int> startSelectedIndex, List<int> endSelectedIndex) {
                                setState(() {});
                              });
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    height: rowHeight,
                    alignment: Alignment.centerLeft,
                    child: Row(
                      children: [
                        Container(
                            width: leftWidth,
                            alignment: Alignment.centerLeft,
                            child: Text.rich(TextSpan(
                                text: '有效时间',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color333)))),
                        Expanded(
                          child: InkWell(
                            child: Container(
                              constraints: const BoxConstraints(minHeight: Sizes.default45),
                              padding: const EdgeInsets.symmetric(horizontal: Sizes.default10),
                              alignment: Alignment.centerLeft,
                              decoration: BoxDecoration(
                                  color: ColorsUtils.rgba(247, 247, 247, 1),
                                  borderRadius:
                                      const BorderRadius.all(Radius.circular(Sizes.default10))),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Expanded(
                                      child: Container(
                                    padding: const EdgeInsets.only(right: Sizes.default10),
                                    child: Text('周一、周二、周三、周五、（7:00-10:00、12:00-14:00、18:-22:00）',
                                        style: StylesUtils.customTextStyle(
                                            fontSize: Sizes.default15,
                                            color: ColorsUtils.color797)),
                                  )),
                                  BrunoTools.getAssetSizeImage(BrnAsset.iconRightArrow, 16, 16)
                                ],
                              ),
                            ),
                            onTap: () {
                              Get.toNamed(RouteGet.groupUseTime, arguments: '添加生效时间');
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    height: rowHeight,
                    alignment: Alignment.centerLeft,
                    child: Row(
                      children: [
                        Container(
                            width: leftWidth,
                            alignment: Alignment.centerLeft,
                            child: Text.rich(TextSpan(
                                text: '使用方式',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color333)))),
                        Expanded(
                          child: Container(
                            alignment: Alignment.centerLeft,
                            child: Text(
                              '移动端',
                              style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                            ),
                          ),
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: Sizes.default20),
              padding:
                  const EdgeInsets.symmetric(horizontal: Sizes.default15, vertical: Sizes.default8),
              decoration: const BoxDecoration(color: Colors.white),
              child: ListBody(
                children: [
                  Container(
                    height: rowHeight,
                    alignment: Alignment.centerLeft,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text('本活动与特价商品优惠同享',
                            style: StylesUtils.customTextStyle(
                                fontSize: Sizes.default15, color: ColorsUtils.color333)),
                        GFToggle(
                          onChanged: (bool? val) {
                            setState(() {
                              favorable = val!;
                            });
                          },
                          value: favorable,
                          type: GFToggleType.ios,
                          boxShape: BoxShape.circle,
                          enabledTrackColor: ColorsUtils.rgba(255, 110, 83, 1),
                          enabledThumbColor: Colors.white,
                        )
                      ],
                    ),
                  ),
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: Sizes.default20),
              padding:
                  const EdgeInsets.symmetric(horizontal: Sizes.default15, vertical: Sizes.default8),
              alignment: Alignment.centerLeft,
              child: Text(
                '温馨提示：活动审核通过后无法修改',
                style: StylesUtils.customTextStyle(
                    fontSize: Sizes.default13, color: ColorsUtils.color999),
              ),
            )
          ],
        ),
      ),
      bottomNavigationBar: SafeArea(
        top: false,
        child: Container(
          height: Sizes.default60,
          alignment: Alignment.center,
          child: FractionallySizedBox(
            widthFactor: 0.8,
            heightFactor: 0.8,
            child: GFButton(
                onPressed: () {},
                text: '保存',
                textStyle: StylesUtils.customTextStyle(fontSize: Sizes.default16),
                shape: GFButtonShape.pills,
                type: GFButtonType.solid,
                size: GFSize.LARGE,
                color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorFf6)),
          ),
        ),
      ),
    );
  }

  BoxDecoration changeBoxDecoration([bool isActive = true]) {
    if (isActive) {
      return BoxDecoration(
          color: ColorsUtils.rgba(255, 238, 238, 1),
          borderRadius: BorderRadius.circular(Sizes.default15),
          border: Border.all(color: ColorsUtils.rgba(255, 110, 83, 1)));
    }
    return BoxDecoration(
      color: ColorsUtils.rgba(245, 245, 245, 1),
      borderRadius: BorderRadius.circular(Sizes.default15),
    );
  }
}
